#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#include <time.h>

void show_holes(int len, int* p);
int my_rand(int a, int b);
void show_num(int len);
void hole_init(int len, int* p);
int ran_mouse(int len,int* p);

void sout(char*str) {
	printf("%s\n",str);
}

void game() {
	int ran_num = my_rand(3, 9);

	int* arr = malloc(ran_num);

	sout("【【==欢迎来到一维打地鼠==】】");
	printf("当前难度：【%d】\n", ran_num);
	hole_init(ran_num, arr);
	sout(">>>洞穴初始化中<<<");
	show_holes(ran_num, arr);
	sout("==========================");

	for (int i = 0; i < ran_num; i++)
	{
		printf("第【%d】次游戏：\n", i + 1);
		int mouse = ran_mouse(ran_num, arr);
	meidazhong:sout("请注意！地鼠出现了！");
		show_holes(ran_num, arr);
		sout("请输入序号进行击打(0-退出)：");
		int input;
		scanf("%d", &input);

		if (input == 0) {
			sout("谢谢再见！");
			return;
		}

		if (input - 1 == mouse) {
			arr[mouse] = -1;
			sout("恭喜你！打中了！");
		}
		else {
			sout("很遗憾...没打中...");
			goto meidazhong;
		}
	}

	sout("@@@@@@@@@@@@@@！全部清除完毕！@@@@@@@@@@@@@@");
	show_holes(ran_num, arr);

}

int main() {
	
	game();

	return 0;
}

void show_holes(int len, int* p) {
	for (int i = 0; i < len; i++)
	{
		printf("↓↓↓↓");
	}
	sout("");

	show_num(len);
	for (int i = 0; i < len; i++)
	{
		if (p[i] == 0) {
			printf("□\t");
		}
		else if (p[i] == -1) {
			printf("■\t");
		}
		else {
			printf("★\t");
		}
		if (i == len - 1) {
			printf("\n");
		}
	}


	for (int i = 0; i < len; i++)
	{
		printf("↑↑↑↑");
	}
	sout("");
}



int my_rand(int a, int b) {
	if (a == 0 && b == 0) {
		return 0;
	}
	srand((unsigned)time(NULL));
	int ran;
	if (a > b) {
		ran = rand() % (a - b + 1) + b;
	}
	else if (a < b) {
		ran = rand() % (b - a + 1) + a;
	}
	else {
		ran = a;
	}
	
	return ran;
}


void hole_init(int len , int *p) {
	
	for (int i = 0; i < len; i++)
	{
		if (p) {
			p[i] = 0;
		}
	}

}


void show_num(int len) {
	for (int i = 0; i < len; i++) {
		printf("%02d\t", i + 1);
		if (i == len - 1) {
			printf("\n");
		}
	}
}

int ran_mouse(int len, int* p) {
	int index = -1;
	while (1) {
		index = my_rand(0, len - 1);
		if (p[index] == 0) {
			p[index] = 1;
			break;
		}
	}
	return index;
}













